import numpy as np
import pandas as pd 
import matplotlib.pyplot as plt
import os 
from matplotlib import rcParams
config = {
            "font.family": 'serif',
            "font.size": 15,
            "mathtext.fontset": 'stix',
            "font.serif": ['SimSun'],
         }
rcParams.update(config)

if False:
    #------------读取原始数据---------------#
    name_file = 'Bearing2_5'
    Learning_set = 'Full_Test_set'
    all_name = os.listdir('PHW2012/{}/'.format(Learning_set)+name_file)
    count_acc = 0
    for i in all_name:
        if 'acc' in i:
            count_acc+=1
    print(count_acc)  # 获取acc的文件个数
    # count_acc=10
    data_BearingX_X=pd.read_csv('PHW2012/{}/{}/acc_00001.csv'.format(Learning_set,name_file),header=None,sep = ',')[[4]]
    data_BearingX_X.columns=[1]  #给没有列明的数据添加列名
    print(data_BearingX_X)

    #----2376个文件（2376个采样）
    co=0
    for i in range(2,count_acc+1):
        j='0'*(5-len(str(i)))+str(i)
        data_new=pd.read_csv('PHW2012/{}/{}/acc_{}.csv'.format(Learning_set,name_file,j),header=None,sep = ',')[[4]]
        data_new.columns=[i]
        data_BearingX_X= pd.concat([data_BearingX_X,data_new],axis=1)
        co+=1
        print(co)


    data_BearingX_X=data_BearingX_X.stack().unstack(0)  #pandas数据的转置
    data_label = pd.DataFrame(np.array(range(count_acc,0,-1)))
    data_label.columns=['label']
    data_label.index=data_label.index+1
    data_BearingX_X_1 = pd.concat([data_BearingX_X,data_label],axis=1)

    data_BearingX_X_1.to_csv('{}.csv'.format(name_file))

    plt.plot(data_BearingX_X,linewidth=0.3)
    #设置坐标轴刻度
    # plt.ylim(-50,50)
    plt.xticks(np.arange(0, count_acc, int(count_acc/5)))
    plt.ylabel('振动程度(A/g)')
    plt.xlabel('采样时间(×10s)')
    plt.grid(linestyle="--")
    # plt.savefig('PHW12_sample2_2.svg')
    plt.savefig('PHW12_{}_{}.png'.format(Learning_set,name_file),bbox_inches='tight',dpi=160)
    plt.show()

# 按照段切分数据并保存为pkl(无重叠)
'''
import pickle
bearing_name = 'Bearing1_7'
df = pd.read_csv('Bearing_data/{}.csv'.format(bearing_name), encoding='utf-8',index_col=0)
print(df)
df.iloc[:,0:-1] = df.iloc[:,0:-1]/np.abs(df.iloc[:,0:-1].values).max()  # 振动幅值归一化
df.iloc[:,-1] = df.iloc[:,-1]/df.iloc[:,-1].values.max()
if df.shape[0]>1:
    arr = np.array_split(df.values[-int(df.shape[0]/20)*20::],int(df.shape[0]/20),axis=0)  # 均匀分割20长度的序列数据，把舍弃前面多余的

    plt.plot(df.iloc[-int(df.shape[0]/20)*20::,0:-1],linewidth=0.3)
    plt.plot(df.iloc[-int(df.shape[0]/20)*20::,-1],linewidth=2,color='r')
    plt.ylabel('振动程度(A/g)')
    plt.xlabel('采样时间(×10s)')
    plt.grid(linestyle="--")
    plt.tight_layout()
    plt.savefig('Bearing_data/PHW12_{}.png'.format(bearing_name),bbox_inches='tight',dpi=160)
    plt.show()

with open('Bearing_data/{}.plk'.format(bearing_name),'wb') as tf:
    pickle.dump(arr,tf)
'''



# 按照段切分数据并保存为pkl(有重叠)

import pickle
for index in [1,2,3,4,5,6,7]:
    bearing_name = 'Bearing1_{}'.format(index)
    df = pd.read_csv('Bearing_data/{}.csv'.format(bearing_name), encoding='utf-8',index_col=0)
    print(df)
    df.iloc[:,0:-1] = df.iloc[:,0:-1]/np.abs(df.iloc[:,0:-1].values).max()  # 振动幅值归一化
    df.iloc[:,-1] = df.iloc[:,-1]/df.iloc[:,-1].values.max()
    if df.shape[0]>1:
        len_split = 40
        arr = np.empty([0,len_split,2561])
        for i in range(0,df.shape[0]-len_split,len_split):
            arr = np.concatenate((arr, df.iloc[i:i+len_split,:].values.reshape(1,40,2561)),axis=0)  

        # plt.plot(df.iloc[-int(df.shape[0]/20)*20::,0:-1],linewidth=0.3)
        # plt.plot(df.iloc[-int(df.shape[0]/20)*20::,-1],linewidth=2,color='r')
        # plt.ylabel('振动程度(A/g)')
        # plt.xlabel('采样时间(×10s)')
        # plt.grid(linestyle="--")
        # plt.tight_layout()
        # plt.savefig('Bearing_data/PHW12_{}.png'.format(bearing_name),bbox_inches='tight',dpi=160)
        # plt.show()

    with open('Bearing_data_40_no/{}.plk'.format(bearing_name),'wb') as tf:
        pickle.dump(arr,tf)


'''
#----------数据归一化-----------#

std_data=pd.read_csv('val_Bearing1_3.csv',index_col=0)  #index_col=0把第一列作为行索引,默认第一行为列索引

n_col=len(list(std_data))  
print(n_col)
for i in range(n_col):
    x=abs(std_data[str(i)])
    std_data[str(i)]=(x-min(x))/(max(x)-min(x))
    
std_data.to_csv('val_Bearing1_3_std.csv')


plt.plot(std_data,linewidth=0.3)
#设置坐标轴刻度
plt.xticks(np.arange(0, 1900, 300))
plt.show()
'''

'''
#----------------训练集和测试集划分-----------#

#对列进行划分

train_test_data=pd.read_csv('Bearing1_3_std.csv',index_col=0)
n_col=len(list(train_test_data)) 

a=[str(i) for i in range(0,int(n_col*0.8))]
b=[str(i) for i in range(int(n_col*0.8),n_col)]

train_data=train_test_data[a]
test_data=train_test_data[b]

train_data.to_csv('Bearing1_3_train_data.csv')
test_data.to_csv('Bearing1_3_test_data.csv')


#按行划分数据集,随机划分
import random
train_test_data=pd.read_csv('Bearing1_3_std.csv',index_col=0)
n_row=np.array(train_test_data).shape[0] 
print(n_row)

test_select_sample=[]
train_select_sample=[]
for i in range(1,n_row+1):
    if i%5==0:
        test_select_sample.append(i)
    else: train_select_sample.append(i)

train_data=train_test_data.loc[train_select_sample]
test_data=train_test_data.loc[test_select_sample]



train_data.to_csv('Bearing1_3_train_data.csv')
test_data.to_csv('Bearing1_3_test_data.csv')



# train_data=test_data.iloc[[1,1,1]]  #iloc使用索引截取
# train_data=test_data.loc[[1,1,1]]  #使用标签截取
# print(train_data)
# size_input=np.array(train_data).shape[1]
# print(size_input)
'''